package com.dr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dr.common.Const;
import com.dr.pojo.ConstantItem;
import com.dr.pojo.Dept;
import com.dr.pojo.RegistLevel;
import com.dr.pojo.User;
import com.dr.service.ConstantService;
import com.dr.service.DeptService;
import com.dr.service.RegistLevelService;
import com.dr.service.UserService;

@Controller
@RequestMapping("/um")
public class UserController {
	
	@Autowired
	private UserService userService;//用户
	@Autowired
	private DeptService deptService;//科室
	@Autowired
	private ConstantService constService;//职称
	@Autowired
	private RegistLevelService levelService;//职称
	/*
	 * 显示用户列表页面，加载所有用户信息
	 */
	@RequestMapping("/userList")
	public ModelAndView userList(User user) {
		List<User> userList = userService.findUserList();//取得用户列表数据	
		ModelAndView mav = new ModelAndView();
		mav.addObject("userList",userList);//加载到用户列表页面中，相当于执行request.setAttribute("userList",userList)
		mav.setViewName("userManage/userList.html");//设定跳转页面
		return mav;
	}
	/*
	 * 打开新增用户页面
	 */
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		//所在科室
		List<Dept> deptList = deptService.findDeptList();	
		//医生职称
		List<ConstantItem> docTitleList =constService.findDocTitleList();
		//挂号级别
		List<RegistLevel> levelList = levelService.findRegistLevel();
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/userAdd");
		mav.addObject("deptList",deptList);		
		mav.addObject("docTitleList",docTitleList);
		mav.addObject("levelList",levelList);
		return mav;		 		
	}
	/*
	 * 执行新增用户
	 */	
	@ResponseBody
	@RequestMapping("/doAdd")
	public Map<String,Object> doAdd(User user) {	
		String userName = user.getUserName();	
		User addUser = userService.findUserByName(userName);
		Map<String,Object> map = new HashMap<>();
		//判断待新增的账户是否存在
		if(addUser!=null) {
			//如果存在
			//提升账户已存在
			//不执行insert,退回新增页面						
			map.put("attrInfoUserAdd",Const.INF_USER_NAME_EXIST);
			map.put("status",Const.STATUS_FAILED);
		}else {
			int result = userService.insertUser(user);
			if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}else {
				map.put("status",Const.STATUS_FAILED);
			}
		}	
		return map;	
	}	
	/*
	 * 打开修改用户页面
	 */
	
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdate(String id) {
		//用户信息
		User user = userService.findUserById(id);
		//所在科室
		List<Dept> deptList = deptService.findDeptList();	
		//医生职称
		List<ConstantItem> docTitleList =constService.findDocTitleList();
		//挂号级别
		List<RegistLevel> levelList = levelService.findRegistLevel();
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/userUpdate.html");
		mav.addObject("attrUser",user);	
		mav.addObject("attrDeptList",deptList);		
		mav.addObject("attrDocTitleList",docTitleList);
		mav.addObject("attrLevelList",levelList);
		return mav;		 		
	}
	/*
	 * 执行修改用户
	 */	
	@ResponseBody
	@RequestMapping("/doUpd")
	public Map<String,Object> doUpd(User user) {			
		Map<String,Object> map = new HashMap<>();
		int result = userService.updateUser(user);
		if(result>0) {
			map.put("status",Const.STATUS_SCUESS);
		}else {
			map.put("status",Const.STATUS_FAILED);
		}
		return map;	
	}	
	 /*
	  * 执行删除用户 
	  */
	 @ResponseBody
	 @RequestMapping("/doDelete") 
	 public  Map<String,Object> doDelete(String... chks) {		  
		 Map<String,Object> map = new HashMap<>(); 
		 int result = userService.deletetUser(chks);	  
		 if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}else {
				map.put("status",Const.STATUS_FAILED);
			}
		 return map;	 
	 }	 
	/*
	* 打开修改密码页面
	 */		
	@RequestMapping("/toPassword")
	public ModelAndView toPassword(String userName) {			
		ModelAndView mav = new ModelAndView();
		mav.setViewName("userManage/pwdUpdate.html");
		mav.addObject("userName",userName);	
		return mav;		 		
	}
	 /*
	  * 执行密码修改
	  */
	 @ResponseBody
	 @RequestMapping("/doPassword") 
	 public  Map<String,Object> doPassword(User user) {		  
		 Map<String,Object> map = new HashMap<>(); 
		 int result = userService.updatePassword(user);  
		 if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}else {
				map.put("status",Const.STATUS_FAILED);
			}
		 return map;	 
	 }	 
}
